package uk.co.fullerlee.vicoid20;

import java.util.Observable;
import java.util.Observer;

import uk.co.fullerlee.vicoid20.widget.Vicoid20ScreenView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import emulator.VicSmall;
import emulator.hardware.CPU;
import emulator.hardware.CPUEventHandler;
import emulator.hardware.clock.TickScales;

public class Vicoid20Main extends Activity {
	
	public static String LOGTAG = "VICOID20";
	
	private Vicoid20ScreenView screenView;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Log.d(Vicoid20Main.LOGTAG, "Starting Vicoid20Main activity");
        setTheme(R.style.MyTheme);
        
        try {
			VicSmall.getInstance().initialise();
		} catch (Exception e) {
			Log.e(Vicoid20Main.LOGTAG, "Failed to initialise", e);
			finish();
		}
		VicSmall.getInstance().getCpu().setEventHandler(new CpuEventHandler());
		VicSmall.getInstance().getBoard().getCpuClock().addClockSpeedObserver(new ClockSpeedObserver());
		VicSmall.getInstance().startCpu();
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN );
        
        setContentView(R.layout.main);
        
        screenView = (Vicoid20ScreenView)findViewById(R.id.vwScreenView);
        screenView.setActivity(this);
        
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(screenView, 0);
    }
    
    private static final int MENU_EXIT = 1;
    private static final int MENU_PRINT_STATE = 2;
    private static final int MENU_START_CPU = 3;
    private static final int MENU_RESET_CPU = 4;
	
	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_EXIT, 1, "Exit");
		menu.add(0, MENU_PRINT_STATE, 2, "Stop CPU");
		menu.add(0, MENU_START_CPU, 2, "Start CPU");
		menu.add(0, MENU_RESET_CPU, 2, "Reset CPU");
	    return true;
	}
	
	
	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case MENU_EXIT:
	    	screenView.finish();
	    	finish();
	    	break;
	    case MENU_PRINT_STATE:
	    	VicSmall.getInstance().printState();
	    	break;
	    case MENU_START_CPU:
	    	VicSmall.getInstance().startCpu();
	    	break;
	    case MENU_RESET_CPU:
	    	VicSmall.getInstance().resetCpu();
	    }
	    
	    return false;
	}
	
	@Override
	protected void onDestroy() {
		Log.d(Vicoid20Main.LOGTAG, "onDestroy - Vicoid20Main");
		
		super.onDestroy();
		
	}
	@Override
	protected void onPause() {
		Log.d(Vicoid20Main.LOGTAG, "onPause - Vicoid20Main");
		
		super.onPause();
	}
	
	protected void onResume() {
		Log.d(Vicoid20Main.LOGTAG, "onResume - Vicoid20Main");
		
		super.onResume();
	}
	
	public class CpuEventHandler implements CPUEventHandler {

		@Override
		public void notify(CPU cpu, String message) {
			Log.w(Vicoid20Main.LOGTAG, message);
		}

	}

	public class ClockSpeedObserver implements Observer {

		@Override
		public void update(Observable arg0, Object arg1) {
			try {
				TickScales scales = (TickScales)arg0;
				Log.d(Vicoid20Main.LOGTAG, "Current clock speed: " + scales.getClockSpeed() + "Hz");
			}
			catch (Exception e)
			{ }
		}

	}

}
